const Packet = require("../packet");
const {stringToBytes, bytes2string} = require("../utils");
class ImageRequestPacket {
    constructor() {
    }

    build(v, request_type, timestamp, image_type, image_file_name) {
        const packet = new Packet();
        packet
            .fill(v, 4)
            .fill(0, 20)
            .fill(request_type, 8)
            .fill(timestamp, 32)
            .fill(image_type, 4)
            .fill(image_file_name.length * 8, 28)
            .fillChunk(stringToBytes(image_file_name), image_file_name.length * 8);
        return new Uint8Array(packet.getPacket());
    }

    parse(p) {
        let v,
            reserved,
            request_type,
            timestamp,
            image_type,
            image_file_name_size,
            image_file_name;
        const packet = new Packet(p);
        v = packet.extract(4);
        reserved = packet.extract(20)
        request_type = packet.extract(8);
        timestamp = packet.extract(32);
        image_type = packet.extract(4);
        image_file_name_size = packet.extract(28);
        image_file_name = bytes2string(packet.extractChunk(image_file_name_size));

        return {
            v,
            reserved,
            request_type,
            timestamp,
            image_type,
            image_file_name
        }
    }
}

module.exports = new ImageRequestPacket();